﻿
#region csMake3D, Copyright(C) 2008 Stefano Vena, Licensed under MIT License.
/*	csMake3D a library for making complex 3D objects
	Copyright (C) 2008
	Stefano Vena
	stefano.vena@gmail.com
	http://code.google.com/p/csmake3d/
    Version 0.0.1

The MIT License

Copyright (c) 2008 Stefano Vena

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using csMake3D.core;


namespace csMake3D.utils.helpers {
    public class Manipulators {
        
        
        //This Method split the src Vertices array and the indices array into four new arrays.Two of them store the vertices(dst1 and dst2) and two store the indices.
        public static void split(Vertex[] src, int[] indices, out Vertex[] dst1, out int[] indicesDst1, out Vertex[] dst2, out int[] indicesDst2) {
            int middle = src.Length / 2;
            List<Vertex> vtx1 = new List<Vertex>();
            List<Vertex> vtx2 = new List<Vertex>();

            List<int> id1 = new List<int>();
            List<int> id2 = new List<int>();

            int lastIndex = 0;

            for (int i = 0; i < indices.Length; i += 3) {

                int A = indices[i];
                int B = indices[i + 1];
                int C = indices[i + 2];
                lastIndex = i;
                if (A < middle && B < middle && C < middle) {
                    id1.Add(A);
                    id1.Add(B);
                    id1.Add(C);

                }
                else {
                    id2.Add(A);
                    id2.Add(B);
                    id2.Add(C);
                }
            }

            int[] aggiunti = new int[src.Length];
            for (int i = 0; i < aggiunti.Length; i++) {
                aggiunti[i] = -1;
            }

            for (int i = 0; i < id1.Count; i++) {
                if (aggiunti[id1[i]] == -1) {
                    aggiunti[id1[i]] = vtx1.Count;
                    vtx1.Add(src[id1[i]]);
                }
            }

            indicesDst1 = new int[id1.Count];
            for (int k = 0; k < id1.Count; k++) {
                indicesDst1[k] = aggiunti[id1[k]];
            }

            for (int i = 0; i < aggiunti.Length; i++) {
                aggiunti[i] = -1;
            }
            for (int i = 0; i < id2.Count; i++) {
                if (aggiunti[id2[i]] == -1) {
                    aggiunti[id2[i]] = vtx2.Count;
                    vtx2.Add(src[id2[i]]);
                }
            }



            indicesDst2 = new int[id2.Count];
            for (int k = 0; k < id2.Count; k++) {
                indicesDst2[k] = aggiunti[id2[k]];
            }

            dst1 = new Vertex[vtx1.Count];
            vtx1.CopyTo(dst1);

            dst2 = new Vertex[vtx2.Count];
            vtx2.CopyTo(dst2);
        }


        //This Method scale each Vertex Position in src array by a "scale" Factor.
        public static void scale(ref Vertex[] src, Sharp3D.Math.Core.Vector3F scale) {
            foreach (Vertex v in src) {
                v.Pos.X *= scale.X;
                v.Pos.Y *= scale.Y;
                v.Pos.Z *= scale.Z;
            }
        }

        ///This Method Optimizes the size of a spline reducing the number of needed points on the base of the passed radius value and the evolution of the same spline.
        public static Sharp3D.Math.Core.Vector3FArrayList OptimizeSpline(Sharp3D.Math.Core.Vector3FArrayList Spline, float radius) {
            //Optimize Spline
            if (Spline.Count == 0) return Spline;            
            float sqradius = radius * radius;

            Sharp3D.Math.Core.Vector3FArrayList newSpline = new Sharp3D.Math.Core.Vector3FArrayList();

            newSpline.Add(Spline[0].Clone());
            int last = 0;
            bool previousAdded = true;

            for (int i = 1; i < Spline.Count; i++) {
                bool near = (Spline[i] - newSpline[last]).GetLengthSquared() < sqradius;
                //If Absolute value of cosine is equals to 1 the angle between vectors is 0 or 180.
                bool line = Math.Abs(Sharp3D.Math.Core.Vector3F.DotProduct(Spline[i], newSpline[last])) == 1;

                if (!near) {
                    if (!line) {
                        //if (!previousAdded) { newSpline.Add(Spline[i - 1].Clone()); last++; }
                        newSpline.Add(Spline[i].Clone());
                        previousAdded = true;
                        last++;
                    }
                    else previousAdded = false;
                }
                else previousAdded = false;



            }

            return newSpline;
        }


        //This Methods calculate the bounding box of an object decribed in src array of vertices.
        public static void getBounds(ref Vertex[] src, out Sharp3D.Math.Core.Vector3D Min, out Sharp3D.Math.Core.Vector3D Max) {

            Max = new Sharp3D.Math.Core.Vector3D(float.MinValue, float.MinValue, float.MinValue);
            Min = new Sharp3D.Math.Core.Vector3D(float.MaxValue, float.MaxValue, float.MaxValue);

            foreach (Vertex v in src) {
                Min.X = Math.Min(v.Pos.X, Min.X);
                Min.Y = Math.Min(v.Pos.Y, Min.Y);
                Min.Z = Math.Min(v.Pos.Z, Min.Z);

                Max.X = Math.Max(v.Pos.X, Max.X);
                Max.Y = Math.Max(v.Pos.Y, Max.Y);
                Max.Z = Math.Max(v.Pos.Z, Max.Z);
            }           
        }

        //This Methods return the Origin and the Length of each axis of object contained in src
        //Note: The origin is the lowest-left point in the object.
        public static void getAxesLength(ref Vertex[] src, out Sharp3D.Math.Core.Vector3D Origin, out Sharp3D.Math.Core.Vector3D Length) {

            getBounds(ref src, out Origin, out Length);

            Length.X = (Length.X - Origin.X);
            Length.Y = (Length.Y - Origin.Y);
            Length.Z = (Length.Z - Origin.Z);
        }

        //This Methods return the Origin and the Inverse Length of each axis of object contained in src
        //Note: The origin is the lowest-left point in the object.
        public static void getInverseAxesLength(ref Vertex[] src, out Sharp3D.Math.Core.Vector3D Origin, out Sharp3D.Math.Core.Vector3D Length) {

            getBounds(ref src, out Origin, out Length);

            Length.X = 1.0f / (Length.X - Origin.X);
            Length.Y = 1.0f / (Length.Y - Origin.Y);
            Length.Z = 1.0f / (Length.Z - Origin.Z);
        }
    }
}
